#include "ReturnGraph.h"
#include "base/Utility.h"
#include "InputPin.h"
#define SetAttr(AttrName) SetAttribute(AttrName, ele->Attribute(AttrName));
bool ReturnGraph::OnInit()
{
    return true;
}

void ReturnGraph::ParseXmlElement(TiXmlElement* ele)
{
    SetAttr("id")
    SetAttr("pos")
    SetAttr("size")
}

bool ReturnGraph::SetAttribute(const char* attrName, const char* attrVal)
{
    if (strcmp("name", attrName) == 0)
    {
        graphName = attrVal;
    }
    else if (strcmp("id", attrName) == 0)
    {
        this->graphId.val = atoi(attrVal);
    }
    else if (strcmp("pos", attrName) == 0)
    {
        int posX, posY;
        sscanf_s(attrVal, "%d,%d", &posX, &posY);
        pos.x.val = posX;
        pos.y.val = posY;
    }
    else if (strcmp("size", attrName) == 0)
    {
        int x, y;
        sscanf_s(attrVal, "%d,%d", &x, &y);
        size.x.val = x;
        size.y.val = y;
    }
    return true;
}

void ReturnGraph::OnDraw()
{
    if (ip == nullptr)
    {
        ip = NewObj<InputPin>();
        ip->gbParent = this;
    }
    glPushMatrix();
    glTranslatef(baseOffset.x.val, baseOffset.y.val, zIndex.val);
    glLineWidth(1);
    DrawBorder();
    //glColor3f(1, 1, 1);
    //glBegin(GL_LINE_LOOP);
    //// -6,-4 6,4 18,0
    //glVertex3f(pos.x.val  + 20 - 6, pos.y.val + 15 - 4, 0);
    //glVertex3f(pos.x.val  + 20 + 6, pos.y.val + 15 - 4, 0);
    //glVertex3f(pos.x.val  + 20 + 18, pos.y.val + 15, 0);
    //glVertex3f(pos.x.val  + 20 + 6, pos.y.val + 15 + 4, 0);
    //glVertex3f(pos.x.val  + 20 - 6, pos.y.val + 15 + 4, 0);
    //glEnd();
  
    ip->pos.x = pos.x.val + 20;
    ip->pos.y = pos.y.val + 15;
    ip->Draw();

    glColor3f(1, 1, 1);
    std::wstring wstr = Utility::ConvertToWstring(graphName.val);
    wchar_t* str = const_cast<wchar_t*>(wstr.data());
    TrueTypeFont::GetInst()->drawText(str, pos.x.val + size.x.val / 4, pos.y.val + 50,zIndex.val, size.x.val, 20);
    glPopMatrix();
}

void ReturnGraph::onMouseButtonDown(int button, int x, int y)
{
    __super::onMouseButtonDown(button, x, y);

   
}

void ReturnGraph::onMouseMove(int x, int y)
{
    __super::onMouseMove( x, y);
}

void ReturnGraph::onMouseButtonUp(int button, int x, int y)
{
    __super::onMouseButtonUp(button, x, y);
    if (ip != nullptr)
    {
        Vec2vbi point;
        point.x.val = x;
        point.y.val = y;
        if (ip->HitTest(vec2(point.x.val, point.y.val)))
        {
            ip->onMouseButtonUp(button, x, y);
        }
    }

}

BlueprintScriptLibrary::Class* ReturnGraph::GetClass()
{
    return nullptr;
}

Vec2vbi ReturnGraph::GetOutputPinPos(int index)
{
    return Vec2vbi();
}

void ReturnGraph::Execute()
{
    printf("Return\r\n");
    printf("----------------\r\n");
}

FunctionPin* ReturnGraph::GetFunctionPin()
{
    return nullptr;
}

void ReturnGraph::ConnectTo(GraphBase* graph, int index)
{
}
